#include"OneWallWayConatctListener.h"
#include"Box2D/Box2D.h"
#include"OneWayWall.h"
#include"Tool.h"

OneWallWayContactsListener::OneWallWayContactsListener(OneWayWall * app)
{
	this->app = app;
}

OneWallWayContactsListener::~OneWallWayContactsListener()
{
}

void OneWallWayContactsListener::BeginContact(b2Contact * contact)
{
	b2Body* b1 = contact->GetFixtureA()->GetBody();
	b2Body* b2 = contact->GetFixtureB()->GetBody();
	if (b1 == app->player&&b2 == app->ground)
	{
		isPlayerOnGround = true;
	}
	else if (b1 == app->ground&&b2 == app->player)
	{
		isPlayerOnGround = true;
	}
}

void OneWallWayContactsListener::EndContact(b2Contact * contact)
{
	b2Body* b1 = contact->GetFixtureA()->GetBody();
	b2Body* b2 = contact->GetFixtureB()->GetBody();
	if (b1 == app->player&&b2 == app->ground)
	{
		isPlayerOnGround = false;
	}
	else if (b1 == app->ground&&b2 == app->player)
	{
		isPlayerOnGround = false;
	}
}

void OneWallWayContactsListener::PreSolve(b2Contact * contact, const b2Manifold * oldManifold)
{
	b2Body* bd=contact->GetFixtureA()->GetBody();
	b2Body* bd1 = contact->GetFixtureB()->GetBody();
	if (bd->GetUserData() == &app->USER_DATA_FLATFROM)
	{
		b2Vec2 contactPoint = getContactsaPoint(contact);
		if (contactPoint.y>app->player->GetPosition().y)
		{
			contact->SetEnabled(false);
		}
		else
		{
			contact->SetEnabled(true);
		}
	}
}

void OneWallWayContactsListener::isContactWithPlatfrom(b2Contact * contact, b2Body * plat, bool isBeginConatct)
{
	//if (plat->GetUserData() != &app->USER_DATA_FLATFROM)
	//{
	//	return;
	//}
	//b2Vec2 contactPoint = getContactsaPoint(contact);
	//
	//if (isBeginConatct)
	//{
	//	if (contactPoint.y < 0)
	//	{
	//		isPlayerOnGround = true;
	//	}
	//	else
	//	{
	//		contact->SetEnabled(false);
	//	}
	//}
	//else
	//{
	//	
	//}
}

void OneWallWayContactsListener::issContactWithGround(b2Contact * conatct, b2Body * ground, bool isBeginContact)
{
	
}

std::vector<b2Body*> OneWallWayContactsListener::sortByOneBody(b2Contact * contact, std::string targetA)
{
	//std::vector<b2Body*> result;
	//tempBodyA = contact->GetFixtureA()->GetBody();
	//tempBodyB = contact->GetFixtureB()->GetBody();
	//
	return std::vector<b2Body*>();
}

b2Vec2 OneWallWayContactsListener::getContactsaPoint(b2Contact * contact)
{
	b2WorldManifold* wm = new b2WorldManifold;
	contact->GetWorldManifold(wm);
	b2Vec2 contactPoint = wm->points[0];
	return contactPoint;
}
